#!/usr/bin/env bash

PREFIX=$(realpath $(cd "$(dirname "$0")"; pwd))
ARM=armv7l
BUSYBOX=busybox-$ARM

cd $PREFIX
cd /tmp
wget -c https://busybox.net/downloads/binaries/1.28.1-defconfig-multiarch/$BUSYBOX

adb root
adb remount
adb disable-verity
# adb reboot
adb wait-for-device

A=$(adb shell getprop sys.boot_completed | tr -d '\r')

while [ "$A" != "1" ]; do
    sleep 2
    A=$(adb shell getprop sys.boot_completed | tr -d '\r')
done

adb shell input keyevent 82

adb shell "mount -o rw,remount /system"

BIN_PATH=/system/xbin

adb shell "mkdir -p $BIN_PATH"

adb push /tmp/$BUSYBOX $BIN_PATH/busybox

adb shell "chmod 755 $BIN_PATH/busybox;cd $BIN_PATH;./busybox --install ."

adb push ubuntu-18.10.ext4.txz /data/

adb shell "cd /data/;busybox mkdir ubuntu;busybox mount -t ext4 -o loop ubuntu-18.10.ext4 ubuntu;"
